<style>
    .add_user_table{
        border-collapse:separate;
        border-spacing:0px 20px;
        margin:20px auto;
    }
</style>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
        <!-- Begin of toolbar -->
        <div id="adminUser-toolbar">
            <div class="wu-toolbar-button">
                <if condition="!$_SESSION['admin']">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAdd()" plain="true">添加</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-lock" onclick="batcheditadminStatus(0)" plain="true">批量锁定</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-lock" onclick="batcheditadminStatus(1)" plain="true">批量解锁</a>
                </if>
                <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="searchAdminUserGrid()"
                   plain="true">刷新</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="batchresetpwd()" plain="true">批量重置密码</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-back" onclick="removeTab()" plain="true">返回</a>
            </div>
            <div class="wu-toolbar-search">
                <label>用户名：</label><input class="easyui-textbox" type="text" name="adminuser"
                                           data-options="prompt:'用户名'" style="width:240px"></input>
                <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="searchAdminUserGrid()">开始检索</a>
            </div>
        </div>
        <!-- End of toolbar -->
        <table id="adminUser-datagrid" toolbar="#adminUser-toolbar" class="easyui-datagrid">
        </table>
    </div>
</div>
<!-- Begin of easyui-dialog -->
<div id="wu-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'"
     style="width:400px; padding:10px;">
    <form id="user-add-form" method="post">
        <table class="add_user_table">
            <tr>
                <td width="60" align="right">用户名:</td>
                <td><input type="text" name="add_username" class="easyui-textbox" style="width:160px"/></td>
            </tr>
            <tr>
                <td align="right">密码:</td>
                <td><input type="password" name="password" class="easyui-textbox" style="width:160px" /></td>
            </tr>
            <tr>
                <td align="right">确认密码:</td>
                <td><input type="password" name="confirmpwd" class="easyui-textbox" style="width:160px" /></td>
            </tr>
        </table>
    </form>
</div>
<!-- End of easyui-dialog -->
<script type="text/javascript">
    $(function () {
        $('#adminUser-datagrid').datagrid({
            url: "{:U('User/getAdminUserListByPage','','')}",
            rownumbers: true,
            pagination: true,
            fitColumns: true,
            fit: true,
            singleSelect: false,
            pageSize: 10,
            pageList: [5, 20, 50],
            remoteSort: true,
            multiSort: true,
            columns: [[
                {field:'ck',checkbox:"true"},
                {field: 'id', title: 'ID', align: 'center', width: 40},
                {field: 'username', title: '用户名', width: 60, align: 'center'},
                {field: 'admin', title: '级别', width: 60, align: 'center', formatter: showAdmin},
                {field: 'logintime', title: '最后登录时间', width: 150, align: 'center', formatter: format_logintTime},
                {field: 'loginip', title: '最后登录IP', width: 70, align: 'center'},
                {field: 'lock', title: '账号状态', width: 60, align: 'center', formatter: showLock},
                {field: 'operation', title: '操作', width: 180, align: 'center', formatter: showAdminBtn}
            ]],
            queryParams: {
                username: ""
            },
            onLoadSuccess: function (data) {
                admin="{:session('admin')}";
                if (admin ==0){
                    $("a[name='pwd']").linkbutton({plain: true, iconCls: 'icon-edit'});
                    $("a[name='lock']").linkbutton({plain: true, iconCls: 'icon-lock'});
                }else{
                    $("a[name='pwd']").linkbutton({plain: true, iconCls: 'icon-edit'});
                }
            }
        });
    });

    //按条件查询表格数据
    function searchAdminUserGrid() {
        var queryParams = $('#adminUser-datagrid').datagrid('options').queryParams;
        queryParams.username = $.trim($('input[name=adminuser]').val());
        $('#adminUser-datagrid').datagrid('reload');
    }

    //显示级别
    function showAdmin(value, row, index) {
        if (value == 0) {
            return "超级管理员";
        } else {
            return "管理员";
        }
    }

    //显示状态
    function showLock(value, row, index) {
        if (value == 0) {
            return "正常";
        } else {
            return "锁定";
        }
    }

    //显示操作按钮
    function showAdminBtn(value, row, index) {
        //按条件显示相应按钮
        admin="{:session('admin')}";
        if (admin == 0){
            if(row['lock'] == 0){
                return "<a href='#' onclick='resetpwd(" + index + ")' class='easyui-linkbutton' name='pwd'>重置密码</a>&nbsp;" +
                    "<a href='#' onclick='editadminStatus(" + index + ")' class='easyui-linkbutton' name='lock'>锁定用户</a>";
            }else{
                return "<a href='#' onclick='resetpwd(" + index + ")' class='easyui-linkbutton' name='pwd'>重置密码</a>&nbsp;" +
                    "<a href='#' onclick='editadminStatus(" + index + ")' class='easyui-linkbutton' name='lock'>解除锁定</a>";
            }
        }else{
            return "<a href='#' onclick='resetpwd(" + index + ")' class='easyui-linkbutton' name='pwd'>重置密码</a>&nbsp;" ;
        }
    }

    $('#adminUser-datagrid').datagrid({
        onClickRow: function (rowIndex, rowData) {
            // alert("表格行中的单击");
        }
    })

    //重置密码
    function resetpwd(rowIndex) {
        rows = $('#adminUser-datagrid').datagrid('getSelections');
        if (rows.length != 1){
            return;
        }
        //获取当前选中行的索引
        selectedRowIndex = $('#adminUser-datagrid').datagrid('getRowIndex', $('#adminUser-datagrid').datagrid('getSelected'));
        //判断当前按钮所在行与当前选中行是否一致
        if (rowIndex == selectedRowIndex) {
            row = $('#adminUser-datagrid').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: "{:U('User/resetAdminPwd','','')}",
                    type: "post",
                    dataType: "json",
                    data: 'ids=' + row['id'],
                    success: function (result) {
                        $.messager.show({
                            title:'系统提示',
                            msg:result.msg
                        });
                    }, error: function () {
                    }
                });
            }
        }
    }

    //批量重置密码
    function batchresetpwd() {
        rows = $('#adminUser-datagrid').datagrid('getSelections');
        if (rows.length == 0){
            return;
        }
        ids = "";
        for(i=0;i<rows.length;i++)
        {
            ids = ids + rows[i]['id'] + ",";
        }
        ids = ids.substring(0,ids.length-1);
        $.ajax({
            url: "{:U('User/resetAdminPwd','','')}",
            type: "post",
            dataType: "json",
            data: 'ids=' + ids,
            success: function (result) {
                $.messager.show({
                    title:'系统提示',
                    msg:result.msg
                });
                $('#adminUser-datagrid').datagrid('reload');
            }, error: function () {
            }
        });
    }

    //修改系统用户状态
    function editadminStatus(rowIndex){
        rows = $('#adminUser-datagrid').datagrid('getSelections');
        if (rows.length != 1){
            return;
        }
        //获取当前选中行的索引
        selectedRowIndex = $('#adminUser-datagrid').datagrid('getRowIndex', $('#adminUser-datagrid').datagrid('getSelected'));
        //判断当前按钮所在行与当前选中行是否一致
        if (rowIndex == selectedRowIndex) {
            row = $('#adminUser-datagrid').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: "{:U('User/editAdminUserStatus','','')}",
                    type: "post",
                    dataType: "json",
                    data: 'ids=' + row['id'] + '&lock=' + row['lock'],
                    success: function (result) {
                        $.messager.show({
                            title:'系统提示',
                            msg:result.msg
                        });
                        $('#adminUser-datagrid').datagrid('reload');
                    }, error: function () {
                    }
                });
            }
        }
    }

    //批量修改系统用户状态
    function batcheditadminStatus(lock){
        rows = $('#adminUser-datagrid').datagrid('getSelections');
        if (rows.length == 0){
            return;
        }
        ids = "";
        for(i=0;i<rows.length;i++)
        {
            ids = ids + rows[i]['id'] + ",";
        }
        ids = ids.substring(0,ids.length-1);
        $.ajax({
            url: "{:U('User/editAdminUserStatus','','')}",
            type: "post",
            dataType: "json",
            data: 'ids=' + ids + '&lock=' + lock,
            success: function (result) {
                $.messager.show({
                    title:'系统提示',
                    msg:result.msg
                });
                $('#adminUser-datagrid').datagrid('reload');
            }, error: function () {
            }
        });
    }

    //格式化日期列
    function format_logintTime(value, row, index) {
        return toDateString(value * 1000);
    }

    //添加记录
    function add() {
        // $('#user-add-form').form('submit', {
        //     url: '',
        //     success: function (data) {
        //         if (data) {
        //             $.messager.alert('信息提示', '提交成功！', 'info');
        //             $('#wu-dialog').dialog('close');
        //         }
        //         else {
        //             $.messager.alert('信息提示', '提交失败！', 'info');
        //         }
        //     }
        // });
        if($.trim($('input[name=add_username]').val())==""){
            $.messager.show({
                title:'系统提示',
                msg:"用户名没有填写"
            });
            $('input[textboxname=add_username]').textbox().next('span').find('input').focus();
            return;
        }

        if($.trim($('input[name=password]').val())==""){
            $.messager.show({
                title:'系统提示',
                msg:"密码没有填写"
            });
            $('input[textboxname=password]').textbox().next('span').find('input').focus();
            return;
        }
        if($.trim($('input[name=confirmpwd]').val())==""){
            $.messager.show({
                title:'系统提示',
                msg:"确认没有填写"
            });
            $('input[textboxname=confirmpwd]').textbox().next('span').find('input').focus();
            return;
        }
        if($.trim($('input[name=confirmpwd]').val())!=$.trim($('input[name=password]').val())){
            $.messager.show({
                title:'系统提示',
                msg:"密码与确认密码不一致"
            });
            $('input[textboxname=confirmpwd]').textbox().next('span').find('input').focus();
            return;
        }
        var msg,isExist;
        $.ajax({
            url: "{:U('User/checkAdminUser','','')}",
            type: "post",
            async: false, //必须使用同步方式
            dataType: "json",
            data: 'username=' + $.trim($('input[name=add_username]').val()),
            success: function (result) {
                debugger;
                msg = result.msg;
                isExist = result.isExist;
            }, error: function () {
            }
        });
        if(isExist){
            $.messager.show({
                title:'系统提示',
                msg:msg
            });
            $('input[textboxname=add_username]').textbox().next('span').find('input').focus();
            return;
        }

        $("#user-add-form").ajaxSubmit({
            url: "{:U('User/SaveAdminUser','','')}",
            type: "post",
            dataType: "json",
            beforeSubmit: function (arr, $form, options) {
            },
            //提交成功后的回调函数
            success: function (result, status, xhr, $form) {
                $.messager.show({
                    title:'系统提示',
                    msg:result.msg,
                    timeout:500,
                    showType:'slide'
                });
                if(result.errorCode == 0){
                    // $('#wu-dialog').dialog('close');
                    $('input[textboxname=add_username]').textbox('setValue','');
                    $('input[textboxname=confirmpwd]').textbox('setValue','');
                    $('input[textboxname=password]').textbox('setValue','')
                    $('input[textboxname=add_username]').textbox().next('span').find('input').focus();
                    $('#adminUser-datagrid').datagrid('reload');
                }
            },
            error: function (xhr, status, error, $form) {
            },
            complete: function (xhr, status, $form) {
            }
        });
    }

    // 打开添加窗口
    function openAdd() {
        $('#user-add-form').form('clear');
        $('#wu-dialog').dialog({
            closed: false,
            modal: true,
            title: "添加系统用户",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: add
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#wu-dialog').dialog('close');
                }
            }]
        });
    }
</script>